package com.rw.coco.expression;

import com.rw.tool.util.string.StringScanner;
import com.rw.tool.util.string.StringUtil;
import lombok.Getter;

import java.util.regex.Pattern;

@Getter
public class RollCommandParser {
    private static final String EXPRESSION_REGEX = "[\\-+*/×÷（）()0-9dfkq]*";

    public static final Pattern EXPRESSION_PATTERN = Pattern.compile(EXPRESSION_REGEX, Pattern.CASE_INSENSITIVE);

    private static final String DEFAULT_EXPRESSION = "1D100";

    private final String expression;

    private final String name;

    public RollCommandParser(String command) {
        StringScanner scanner = new StringScanner(command);

        // 取出表达式
        if (scanner.hasNext(EXPRESSION_PATTERN)) {
            expression = scanner.next(EXPRESSION_PATTERN);
        } else {
            expression = DEFAULT_EXPRESSION;
        }

        // 去除空格
        if (scanner.hasNext(" +")) {
            scanner.next(" +");
        }

        // 取出名字
        String name = scanner.nextAll();
        if (StringUtil.isBlank(name)) {
            name = StringUtil.EMPTY;
        }

        this.name = name;
    }
}
